<?php

class Book_manage  extends AdminManage
{
	private $_pagesize=10;//分页个数
	/**
	 * 初始化
	 */
	public function _init()
	{

	}

	/**
	 * 默认动作
	 */
	protected function _default()
	{

		$strWhere='id > 0';
		#获取数据总数
		$total = DB::getRows('ebook',$strWhere);


		/**分页操作 start**/
		$p = 1; //当前页码
		if (isset($_GET['p']) && is_numeric($_GET['p'])) {
			$p =intval($_GET['p']);
			if ($p<=1) {
				$p = 1;
			}
		}

		$totalPage = ceil($total/$this->_pagesize);//计算总页数
		$begin = ($p-1)*$this->_pagesize;//计算开始游标
		$limit = "$begin,$this->_pagesize";//计算分页



		$pageObj = new Dpage($total, $this->_pagesize, $p);
		$pageCss = $pageObj->pageCss("1");
		$pageform = $pageObj->outputPages(1,"<li>","</li>");

		/**分页操作 end**/

//DB::echoExeSql();
		#获取数据信息
		$list = new publicGetDataModel();
		$listData = $list->getData($where = $strWhere,$limit = $limit,$fields = 'id,name,language,filename,status,dateline,path',$order = 'dateline',$table='ebook');
		
	//print_r($listData);



		#模板赋值
		$now=time();
		$this->assign('now',$now);
		$this->assign('list',$listData);
		$this->assign('pageCss',$pageCss);//分页样式
		$this->assign('pageform',$pageform);//分页标签


		$this->assign('menu_navbar','内容管理  -- <a href="'.$_GET['action'].'.html">电子书列表</a>');
		$this->tpl_full_path('/admins/book_manage/index');
	}
	
	
	
	/**
	**添加电子书
	**/
	public function _add(){
		set_time_limit(0);
		if($_SERVER["REQUEST_METHOD"] === "POST" ){
			$language = $this->getParam('language','i');
			$name = $this->getParam("name",'t');
			if(!$name){
				$this->alertBack("请输入文件名");
			}
			/*
			if($_FILES['addfile']){
				$fname = $_FILES["addfile"]["name"];
				$ftype = strtolower(substr($fname,strrpos($fname,'.')+1));
				$allowtype = array('jpg','jpeg','gif','png');
				if(!in_array($ftype,$allowtype)) {
					$this->alertBack("只允许上传图片！");
				}else{
					if(!is_uploaded_file($_FILES["addfile"]['tmp_name'])){  //如果不是通过HTTP POST上传的 
						$this->alertBack("添加失败，请重试");
					}else{
						if($_FILES["addfile"]["size"] > 2000000){
							$this->alertBack("文件不小不能超过2M");
						}else{
							$fnewname = time().'.'.$ftype;
							if(move_uploaded_file($_FILES["addfile"]["tmp_name"],"uploads/" . $fnewname)){
								$path = "uploads/" . $fnewname;
							}else{
								$this->alertBack("添加失败，请重试");
							}
						}
					}
				}
			}else{
				$this->alertBack("请添加附件！");
			}
			*/
			if($_FILES['addfile']){
				$oImage = new Upload('ebook','2048');
				
				
				$oImage->BUILD_IMG['file']['s'][] = array(100,100);
				$oImage->BUILD_IMG['file']['n'][] = 's_';
				$imagePath = $oImage->upFiles();
				$pathReg='/^\/uploads\/users/';
				if(preg_match($pathReg,$imagePath)){
					$newImagePath = $imagePath;
				}else{
					$this->alertBack("上传失败，图片不能超过2M");
				}
			}else{
				$this->alertBack("请添加附件！");
			}
			
			$inDB['language'] = $language;
			$inDB['name'] = $name;
			$inDB['path'] =  $newImagePath;
			$inDB['filename'] = $name;
 			$inDB['dateline'] = time();
			if(DB::insert('ebook',$inDB)){
				$this->alertBack("添加成功！");
			}else{
				$this->alertBack("添加失败,请重试！");
			}
		}
	}

	/**
	 * 删除
	 */
	protected function _changeStatus(){

		$id = $this->getParam('id','t');
		$status = $this->getParam('status','i');
		if(isset($id) && !empty($id) ){
			if($status==1){
				$upDB['status']=2;
			}else{
				$upDB['status']=1;
			}
			$result = DB::update('ebook','id='.$id,$upDB);
			if($result){
				die('1');
			} else {
				die('0');
			}
		}else{ //参数错误
			die('2');
		}
	}

	
	protected function _changeOrder(){
		$id = $this->getParam('id','t');
		$order = $this->getParam('order','i');
		if(isset($id) && !empty($id) ){
			$upDB['language']=$order;
			$result = DB::update('ebook','id='.$id,$upDB);
			if($result){
				die('1');
			} else {
				die('0');
			}
		}else{ //参数错误
			die('2');
		}
	}

}
?>
